import { takeEvery } from "redux-saga/effects";
import { WarehouseQueryApi } from "@api";
import {
  SAGA_WAREHOUSE_QUERY_LOAD_ITEM_DETAILS,
  SAGA_WAREHOUSE_QUERY_LOAD_LOC_DETAILS,
} from "./saga-types";
import { asyncRequest } from "@common/saga-helper";
import {
  SET_WAREHOUSE_QUERY_ITEM_DETAILS,
  SET_WAREHOUSE_QUERY_LOC_DETAILS,
} from "./action-types";

function* loadWarehouseQueryItemDetailsSaga() {
  yield takeEvery(SAGA_WAREHOUSE_QUERY_LOAD_ITEM_DETAILS, function* (action) {
    yield* asyncRequest({
      effects: [
        {
          api: WarehouseQueryApi.loadItemDetails,
          respActions: [SET_WAREHOUSE_QUERY_ITEM_DETAILS],
          payload: action.payload,
        },
      ],
    });
  });
}

function* loadWarehouseQueryLocDetailsSaga() {
  yield takeEvery(SAGA_WAREHOUSE_QUERY_LOAD_LOC_DETAILS, function* (action) {
    yield* asyncRequest({
      effects: [
        {
          api: WarehouseQueryApi.loadLocDetails,
          respActions: [SET_WAREHOUSE_QUERY_LOC_DETAILS],
          payload: action.payload,
        },
      ],
    });
  });
}

export default [
  loadWarehouseQueryItemDetailsSaga(),
  loadWarehouseQueryLocDetailsSaga(),
];
